package cn.message.service.impl;

import cn.itcast.mapper.helpCenterMapper;
import cn.itcast.pojo.helpCenter;
import cn.itcast.pojo.helpCenterExample;
import cn.message.service.IHelpCenterService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-05-14 8:34
 */
@Service
@Slf4j
public class HelpCenterServiceImpl implements IHelpCenterService {

    @Resource
    private helpCenterMapper helpCenterMapper;

    @Override
    public Map<String,Object> getAllHelp(Map<String, String> map) {
        helpCenterExample helpCenterExample = new helpCenterExample();
        cn.itcast.pojo.helpCenterExample.Criteria criteria = helpCenterExample.createCriteria();
        if(!map.get("question").isEmpty()){
            criteria.andQuestionLike("%"+map.get("question")+"%");
        }

        //获取总记录数
        int count = helpCenterMapper.countByExample(helpCenterExample);

        int page = Integer.parseInt(map.get("page"));
        int limit = Integer.parseInt(map.get("limit"));
        //开始分页
        PageHelper.startPage(page,limit);
        List<helpCenter> helpCenterList = helpCenterMapper.selectByExample(helpCenterExample);

        for (helpCenter helpCenter : helpCenterList) {
            log.debug("问题："+helpCenter.getQuestion());
            log.debug("答案："+helpCenter.getSolution());
        }

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("helpCenterList",helpCenterList);
        resultMap.put("count",count);
        return resultMap;
    }

    @Override
    public Boolean addHelp(Map<String, String> map) {
        String question = map.get("question");
        String solution = map.get("solution");
        helpCenter helpCenter = new helpCenter();
        helpCenter.setQuestion(question);
        helpCenter.setSolution(solution);
        int i = helpCenterMapper.insertSelective(helpCenter);
        if(i != 1){
            return false;
        }
        return true;
    }

    @Override
    public Boolean updateHelp(Map<String, String> map) {
        int id = Integer.parseInt(map.get("id"));
        log.debug(map.get("id"));
        log.debug(map.get("question"));
        log.debug(map.get("solution"));

        helpCenter helpCenter = new helpCenter();
        if(!map.get("question").isEmpty()){
            helpCenter.setQuestion(map.get("question"));
        }
        helpCenter.setId(id);
        if(!map.get("solution").isEmpty()){
            helpCenter.setSolution(map.get("solution"));
        }
        int i = helpCenterMapper.updateByPrimaryKeySelective(helpCenter);
        if(i != 1){
            return false;
        }
        return true;
    }

    @Override
    public Boolean deleteHelp(Map<String, String> map) {
        int id = Integer.parseInt(map.get("id"));
        int i = helpCenterMapper.deleteByPrimaryKey(id);
        if(i != 1){
            return false;
        }
        return true;
    }

    @Override
    public helpCenter getHelpById(Map<String, String> map) {
        int id = Integer.parseInt(map.get("id"));
        helpCenter helpCenter = helpCenterMapper.selectByPrimaryKey(id);
        return helpCenter;
    }
}
